<template>
  <el-dialog title="分配角色" :visible="showRoleDialog">
    <!-- // 分配角色 -->
    <div>
      <el-checkbox-group v-model="roleIds">
        <!-- 注意：label决定当前选中的值 -->
        <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
          {{ item.name }}
        </el-checkbox>
      </el-checkbox-group>

      <div style="margin-top: 20px; text-align: right">
        <el-button type="primary" @click="hSave">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </div>
  </el-dialog>
</template>
<script>
import { getEnableRoleList,getEmployeeDetail, assignRoles } from '@/api/employee'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    curEmployeId: {
      type: Number,
      default: ''
    }
  },
  data() {
    return {
      roleList: [], // 接收角色列表
      roleIds: [] // 用来双向绑定数据的
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async loadRoles() {
      const data = await getEnableRoleList()
      this.roleList = data
    },
    async getUserRole() {
      const { roleIds } = await getEmployeeDetail(this.curEmployeId)
      this.roleIds = roleIds
    },
    closeDialog() {
      this.$emit('update:showRoleDialog', false)
    },
    // 保存当前角色信息
    async hSave() {
      const res = await assignRoles({ id: this.curEmployeId, roleIds: this.roleIds })
      this.$message.success('保存成功')
      this.closeDialog()
    }
  }
}
</script>